package com.neusoft.neuhealth.controller;

import com.neusoft.neuhealth.domain.User;
import com.neusoft.neuhealth.po.user.*;
import com.neusoft.neuhealth.po.Result;
import com.neusoft.neuhealth.po.user.result.RoleNumResult;
import com.neusoft.neuhealth.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    // 登录
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<LoginResponse> login(@RequestBody UserLogin user) {
        return userService.login(user);
    }
    // 查询用户
    @Operation(summary = "查询用户列表")
    @GetMapping("/list")
    public Result<UserListResult> list( UserList user) {
        return userService.list(user);
    }
    // 增加用户
    @Operation(summary = "增加用户")
    @PostMapping("/add")
    public Result add(@RequestBody UserAdd user) {
        return userService.add(user);
    }
    // 删除用户
    @Operation(summary = "删除用户")
    @PostMapping("/del")
    public Result delete(@RequestBody Map<String, List<String>> payload) {
        List<String> userNames = payload.get("userNames");
        return userService.delete(userNames);
    }
    // 修改用户
    @Operation(summary = "修改用户信息")
    @PostMapping("/update")
    public Result update(@RequestBody UserAdd user) {
        return userService.update(user);
    }

    // 获取用户总数
    @Operation(summary = "获取用户总数")
    @GetMapping("/count")
    public Result<Long> count() {
        return userService.count();
    }

    @Operation(summary = "获取各角色用户数量")
    @GetMapping("/roleNum")
    public Result<List<RoleNumResult>> roleNum() {
        return userService.roleNum();
    }
    //上传头像
    @Operation(summary = "上传用户头像")
    @PostMapping("/avatar/upload")
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file,@RequestHeader(value = "Authorization", required = false) String token) {
        return userService.uploadFile(file,token);
    }
    //获取用户头像
    @Operation(summary = "获取用户头像")
    @GetMapping("/avatar/get")
    public Result<String> getAvatar(@RequestHeader(value = "Authorization", required = false) String token) {
        return userService.getAvatar(token);
    }
    //获取用户邮箱
    @Operation(summary = "获取用户邮箱")
    @GetMapping("/email/get")
    public Result<String> getEmail(@RequestHeader(value = "Authorization", required = false) String token) {
        return userService.getEmail(token);
    }
}